package pl.matt.media.manager.impl;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import pl.matt.media.model.ImageType;
import pl.matt.model.Rectangle;

/**
 * @author mateusz
 * 
 */

public class AwtImageManager extends AbstractImageManager {

	//@SuppressWarnings("unused")
	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AwtImageManager.class);
	
	@Override
	public boolean cropImage(String sourceImagePath, String destinationImagePath, int x, int y, int width, int height,
			ImageType imageType) throws IOException {
		BufferedImage image = ImageIO.read(new File(sourceImagePath));
		image = image.getSubimage(x, y, width, height);
		return ImageIO.write(image, imageType.getFormatName(), new File(destinationImagePath));
	}
	
	@Override
	public boolean cropImageWithPercentageDimensions(String sourceImagePath, String destinationImagePath, int xPercentage, int yPercentage, int widthPercentage, int heightPercentage,
			ImageType imageType) throws IOException {
		BufferedImage image = ImageIO.read(new File(sourceImagePath));
		int width = image.getWidth();
		int height = image.getHeight();
		image = image.getSubimage(width * xPercentage / 100, height * yPercentage / 100, width * widthPercentage / 100, height * heightPercentage / 100);
		return ImageIO.write(image, imageType.getFormatName(), new File(destinationImagePath));
	}	

	@Override
	public boolean convertToGrayScale(String sourceImagePath, String destinationImagePath, ImageType imageType)
			throws IOException {
		BufferedImage colorImage = ImageIO.read(new File(sourceImagePath));

		
		BufferedImage image = new BufferedImage(colorImage.getWidth(), colorImage.getHeight(),
				BufferedImage.TYPE_BYTE_GRAY);
		Graphics g = image.getGraphics();
		g.drawImage(colorImage, 0, 0, null);
		g.dispose();
		return ImageIO.write(image, imageType.getFormatName(), new File(destinationImagePath));
	}

	@Override
	public Rectangle getImageRectangle(File imageFile) throws IOException {
		BufferedImage image = ImageIO.read(imageFile);
		int width = image.getWidth();
		int height = image.getHeight();
		return new Rectangle(0, 0, width, height);
	}
	
	@Override
	public void divideImage(String sourceImagePath, String destinationImagePath1, String destinationImagePath2,
			boolean horizontal, ImageType imageType) throws IOException {
		LOG.debug(sourceImagePath + " <-- sourceImagePath");
		BufferedImage image = ImageIO.read(new File(sourceImagePath));
		int width = image.getWidth();
		int height = image.getHeight();
		BufferedImage image1, image2;
		if (horizontal) {
			image1 = image.getSubimage(0, 0, width, height/2);
			image2 = image.getSubimage(0, height/2, width, height/2);
		} else {
			image1 = image.getSubimage(0, 0, width/2, height);
			image2 = image.getSubimage(width/2, 0, width/2, height);
		}
		ImageIO.write(image1, imageType.getFormatName(), new File(destinationImagePath1));
		ImageIO.write(image2, imageType.getFormatName(), new File(destinationImagePath2));
	}

}
